window.TK && window.TK.init(window.PROJECT);
window.onload = function () {
	if ("undefined" != typeof mraid) {
		"loading" === mraid.getState()
			? mraid.addEventListener("ready", mraidReadyCallback)
			: mraidReadyCallback();
	} else if (typeof dapi != "undefined") {
		dapi.isReady()
			? onReadyCallback()
			: dapi.addEventListener("ready", onReadyCallback);
	} else {
		window.gameReady && window.gameReady();
		gameStart();
	}
};

function mraidReadyCallback() {
	mraid.addEventListener("viewableChange", viewableChangeHandler);
	if (mraid.isViewable()) {
		gameStart();
	}
}

function viewableChangeHandler(viewable) {
	if (viewable) {
		gameStart();
	}
}

function onReadyCallback() {
	console.info("dapi is ready!");
	dapi.removeEventListener("ready", onReadyCallback);
	window.TK && window.TK.ready();
	if (dapi.isViewable()) {
		adVisibleCallback({ isViewable: true });
		dapi.getAudioVolume();
		dapi.getScreenSize();
		dapi.addEventListener("viewableChange", adVisibleCallback);
		dapi.addEventListener("adResized", adResizeCallback);
		dapi.addEventListener("audioVolumeChange", audioVolumeChangeCallback);
	}
}

function adVisibleCallback(event) {
	window.TK && window.TK.changeViewable();
	if (event.isViewable) {
		gameStart();
		dapi.getScreenSize();
	} else {
		//PAUSE the ad and MUTE sounds
	}
}

function adResizeCallback() {
	window.TK && window.TK.resize();
}

function audioVolumeChangeCallback() {
	let isAudioEnabled = !!dapi.getAudioVolume();
	window.TK && window.TK.changeAudioVolume();
	if (isAudioEnabled) {
		//START or turn on the sound
		window.dispatchEvent(new Event("ccc:unmute"));
	} else {
		//PAUSE the turn off the sound
		window.dispatchEvent(new Event("ccc:muteAll"));
	}
}

window.gameReady && window.gameReady();
function gameStart() { }
function gameClose() { }
